<?php
namespace app\admin\model\goods;
use think\facade\Db;
use app\admin\common;

class Order{
	
	public function findOrderData($_where,$_field){
		return Db::name("shop_order")->alias("o")
			->join("user u","u.id=o.user_id")
			->join("shop_order_addr addr","addr.id=o.addr_id")
			->join("shop_order_details d","d.order_id=o.id")
			->field($_field)
			->where($_where)
			->find();
	}
	
	public function selectOrderData($_where,$_field,$_limit,$_order)
	{
		$_data=Db::name("shop_order")->alias("o")
			->join("user u","u.id=o.user_id")
			->join("shop_order_addr addr","addr.id=o.addr_id")
			->join("shop_order_details d","d.order_id=o.id")
			->field($_field)
			->where($_where)
			->limit(current($_limit),end($_limit))
			->order($_order)
			->select();
		return empty($_data)?:$_data->toArray();	
	}
	
	public function selectUserOrderDetails($_where,$_field)
	{
		$_data=Db::name("shop_order_details")->alias("o")
			->join("shop_goods g","g.id=o.goods_id")
			->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function selectUserAddr($_where,$_field,$_order="addr.id asc")
	{
		$_data=Db::name("shop_order_addr")
				->alias("addr")
				->join("region r","r.region_id=a.province_id")
				->join("region rr","rr.region_id=a.city_id")
				->join("region rrr","rrr.region_id=a.area_id")
				->where($_where)->field($_field)->order($_order)->select();
		return empty($_data)?:$_data->toArray();		
	}
	
	public function findOrder($_where,$_field)
	{
		return Db::name("shop_order")->where($_where)->field($_field)->find();
	}
	
	public function updateOrder($_up,$_where)
	{
		return Db::name("shop_order")->where($_where)->update($_up);
	}
	public function findOrderCourier($_where,$_field)
	{
		return Db::name("shop_order_courier")->alias("c")
			->join("shop_courier_company company","company.id=c.company_id")
			->where($_where)->field($_field)->find();		
	}
	public function insertOrderCourier($_data)
	{
		return Db::name("shop_order_courier")->insert($_data);
	}
	
	public function updateOrderDetails($_up,$_where)
	{
		return Db::name("shop_order_details")->where($_where)->update($_up);
	}
}
?>